package com.haojiu.controller;

import com.github.pagehelper.PageInfo;
import com.haojiu.pojo.Book;
import com.haojiu.pojo.User;
import com.haojiu.service.BookService;
import com.haojiu.service.UserService;
import com.haojiu.util.ExportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:我叫浩九
 * @date：2021/8/25 11:02
 * create in RuGao JiangSu
 */
@Controller
public class UserController {
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    @RequestMapping("/readerIndex")
    public ModelAndView queryAllUser(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size,HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        List<User> list = userService.queryAllUser(page, size);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        mv.addObject("pageInfo", pageInfo);
        HttpSession session = request.getSession();
        String userType =(String) session.getAttribute("userType");
        if(userType.equals("manager")){
            mv.setViewName("reader/allReader");
        }else {
            mv.setViewName("reader/allReader2");
        }
        return mv;
    }

    @RequestMapping("/login/delUser/{username}")
    public String deleteBook(@PathVariable("username") String username) {
        userService.deleteUserByName(username);
        return "redirect:/readerIndex";
    }

    @RequestMapping("/updatePassword")
    public String toUpdateUser() {
        return "reader/updateUser";
    }
    @RequestMapping("/login/toUpdateUser")
    public String toUpdateUser(Model model, String username) {
        User user = userService.queryUserByName(username);
        System.out.println(user);
        model.addAttribute("user", user);
        return "reader/updateUser1";
    }
    @RequestMapping("/login/updateUser1")
    public String updateBook(Model model, User user) {
        userService.updateUser(user);
        User user1 = userService.queryUserByName(user.getUsername());

        model.addAttribute("users", user1);
        return "redirect:/readerIndex";
    }
    @RequestMapping("/login/updateUser")
    public String updateUser(Model model, String password,HttpServletRequest request) {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        String userType = (String) session.getAttribute("userType");
        User user = new User(username, password, userType,null);
        userService.updateUser(user);
        User users = userService.queryUserByName(user.getUsername());
        model.addAttribute("users", users);
        return "redirect:/readerIndex";
    }

    @RequestMapping("/login/toAddUser")
    public String toAddUser() {
        return "reader/addUser";
    }

    @RequestMapping("/login/addUser2")
    public String addUser(User user) {
        System.out.println(user);
        userService.addUser(user);
        return "redirect:/readerIndex";
    }

    @RequestMapping("/login/searchUser")
    public String getUser(@RequestParam("keyUserName") String username, Model model) {
        User user = userService.queryUserByName(username);
        if (user != null) {
            List<User> userList = new ArrayList();
            userList.add(user);
            model.addAttribute("one", userList);
            return "reader/oneReader";
        } else {
            return "redirect:/readerIndex";
        }
    }
    @RequestMapping("/login/exportUser")
    public void export(HttpServletRequest request, HttpServletResponse response){
        List<User> userList = userService.queryAllUser();
        ExportExcel<User> ee= new ExportExcel<User>();
        String[] headers = {"用户名称","登录密码","用户类型"};
        String fileName = "阅读之家用户excel表";
        ee.exportExcel(headers,userList,fileName,response);
    }
}
